//
//  ZLExciseBeginSheetTableViewCell.m
//  ZLExamination
//
//  Created by Lei_Zhong_Xun on 2017/9/5.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExciseBeginSheetTableViewCell.h"
#import "ZLExciseBeginModel.h"

@implementation ZLExciseBeginSheetTableViewCell

#pragma mark - 点击事件
- (void)buttonClickAction:(UIButton *)sender {
    if (_clickCallBack) {
        _clickCallBack(sender.tag-30000);
    }
}

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setNumbersWithRows:(NSInteger)rows withSingles:(NSMutableArray *)singles {
    for (int i = 0; i< rows; i++) {
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor whiteColor];
        CGFloat a = i/5.;
        NSInteger b = i/5+1;
        NSInteger row = a>b? b: b-1;
        
        bgView.frame = CGRectMake(kSCREEN_WIDTH/5*(i%5), kSCREEN_WIDTH/5*row, kSCREEN_WIDTH/5, kSCREEN_WIDTH/5);
        [self.contentView addSubview:bgView];
        
        UIButton *numberBtn;
        
        ZLExciseBeginModel *model = (ZLExciseBeginModel *)singles[i];
        if (model.isAnswer == YES) {
            numberBtn = [self getNumberLblWithStyle:ZLExamBeginSheetCellAnswerStyleAnswered];
        } else {
            numberBtn = [self getNumberLblWithStyle:ZLExamBeginSheetCellAnswerStyleUnAnswered];
        }
        
        numberBtn.tag = 30000+ i;
        
        [numberBtn setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        
        [bgView addSubview:numberBtn];
        
        [numberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(bgView);
            make.width.height.mas_equalTo(35);
        }];
    }
}

- (void)setNumbersWithRows:(NSInteger)rows withMultiple:(NSMutableArray *)Multiple withSingleRows:(NSInteger)singleRows {
    for (int i = 0; i< rows; i++) {
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor whiteColor];
        CGFloat a = i/5.;
        NSInteger b = i/5+1;
        NSInteger row = a>b? b: b-1;
        
        bgView.frame = CGRectMake(kSCREEN_WIDTH/5*(i%5), kSCREEN_WIDTH/5*row, kSCREEN_WIDTH/5, kSCREEN_WIDTH/5);
        [self.contentView addSubview:bgView];
        
        UIButton *numberBtn;
        
        ZLExciseBeginModel *model = (ZLExciseBeginModel *)Multiple[i];
        if (model.answers.count > 0) {
            numberBtn = [self getNumberLblWithStyle:ZLExamBeginSheetCellAnswerStyleAnswered];
        } else {
            numberBtn = [self getNumberLblWithStyle:ZLExamBeginSheetCellAnswerStyleUnAnswered];
        }
        
        numberBtn.tag = 30000+i+singleRows;
        
        [numberBtn setTitle:[NSString stringWithFormat:@"%ld",i+1+singleRows] forState:UIControlStateNormal];
        [bgView addSubview:numberBtn];
        
        [numberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.equalTo(bgView);
            make.width.height.mas_equalTo(35);
        }];
    }
}


- (UIButton *)getNumberLblWithStyle:(ZLExamBeginSheetCellAnswerStyle)style {
    UIButton *numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    numberLbl.textAlignment = NSTextAlignmentCenter;
    numberBtn.layer.cornerRadius = 17.5;
    numberBtn.layer.masksToBounds = YES;
    [numberBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    switch (style) {
        case ZLExamBeginSheetCellAnswerStyleAnswered: {
            numberBtn.layer.backgroundColor = [kMY_BLUE_COLOR CGColor];
            [numberBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
            break;
            
        case ZLExamBeginSheetCellAnswerStyleUnAnswered: {
            numberBtn.layer.backgroundColor = [[UIColor whiteColor] CGColor];
            [numberBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            numberBtn.layer.borderColor = [[UIColor darkGrayColor] CGColor];
            numberBtn.layer.borderWidth = 0.5;
        }
            break;
    }
    
    return numberBtn;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
